package cn.csc.module.order.controller.admin.addr;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.order.controller.admin.addr.vo.*;
import cn.csc.module.order.convert.addr.OrderAddrConvert;
import cn.csc.module.order.dal.dataobject.addr.OrderAddrDO;
import cn.csc.module.order.service.addr.OrderAddrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 用户订单配送地址")
@RestController
@RequestMapping("/order/addr")
@Validated
public class OrderAddrController {

    @Resource
    private OrderAddrService addrService;

    @PostMapping("/create")
    @ApiOperation("创建用户订单配送地址")
    @PreAuthorize("@ss.hasPermission('order:addr:create')")
    public CommonResult<BigInteger> createAddr(@Valid @RequestBody OrderAddrCreateReqVO createReqVO) {
        return success(addrService.createAddr(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户订单配送地址")
    @PreAuthorize("@ss.hasPermission('order:addr:update')")
    public CommonResult<Boolean> updateAddr(@Valid @RequestBody OrderAddrUpdateReqVO updateReqVO) {
        addrService.updateAddr(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户订单配送地址")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:addr:delete')")
    public CommonResult<Boolean> deleteAddr(@RequestParam("id") BigInteger id) {
        addrService.deleteAddr(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户订单配送地址")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('order:addr:query')")
    public CommonResult<OrderAddrRespVO> getAddr(@RequestParam("id") BigInteger id) {
        OrderAddrDO addr = addrService.getAddr(id);
        return success(OrderAddrConvert.INSTANCE.convert(addr));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户订单配送地址列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('order:addr:query')")
    public CommonResult<List<OrderAddrRespVO>> getAddrList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<OrderAddrDO> list = addrService.getAddrList(ids);
        return success(OrderAddrConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户订单配送地址分页")
    @PreAuthorize("@ss.hasPermission('order:addr:query')")
    public CommonResult<PageResult<OrderAddrRespVO>> getAddrPage(@Valid OrderAddrPageReqVO pageVO) {
        PageResult<OrderAddrDO> pageResult = addrService.getAddrPage(pageVO);
        return success(OrderAddrConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户订单配送地址 Excel")
    @PreAuthorize("@ss.hasPermission('order:addr:export')")
    @OperateLog(type = EXPORT)
    public void exportAddrExcel(@Valid OrderAddrExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OrderAddrDO> list = addrService.getAddrList(exportReqVO);
        // 导出 Excel
        List<OrderAddrExcelVO> datas = OrderAddrConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户订单配送地址.xls", "数据", OrderAddrExcelVO.class, datas);
    }

}
